package com.time.workshop.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker.OnTimeChangedListener;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.time.workshop.API;
import com.time.workshop.BaseConstant;
import com.time.workshop.R;
import com.time.workshop.SuperActivity;
import com.time.workshop.bean.RequestOrder;
import com.time.workshop.bean.ResponseShopDetail;
import com.time.workshop.bean.WaimaiType;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.LaoBanDialog;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.OnClickOKListener;

/**
 * @author fanbaolong
 *  提交外卖详情订单
 */
public class WaiMaiOrderActivity extends SuperActivity{

	@V
	private Button waimai_order_titlebar_left_btn; 
	@V
	private RelativeLayout waimai_order_info_Layout,waimai_order_time_Layout,waimai_order_name_Layout,
	waimai_order_phone_Layout,waimai_canting_address_Layout,waimai_order_address_Layout,waimai_order_remarks_Layout;

	@V
	private TextView  waimai_order_commitOrderTextView, waimai_order_timeTextView,waimai_order_shopNameTextView,
	waimai_order_shopAddrTextView,waimai_order_advancePriceTextView;
	@V
	private LinearLayout waimai_order_infoTexts_Layout;
	@V
	private ImageView waimai_order_infoImageView;
	@V
	private EditText waimai_order_nameEditText,waimai_order_phoneEditText,
	waimai_canting_addressEditText,waimai_order_addressEditText,waimai_order_remarksEditText;

	private DatePicker datePicker;  
	private TimePicker timePicker;  
	private AlertDialog dialog;  
	public Calendar time = Calendar.getInstance(Locale.CHINA);  
	private boolean processFlag = true;

	private  LaoBanDialog dlg;

	private WaimaiType waimai;

	private String waimaitime,name,phone,canting_address,address,remark,store_id;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_waimai_order);

		init();


	}

	private void setView(){
		Intent in = getIntent();
		waimai = (WaimaiType) in.getSerializableExtra("waimai");
		store_id = waimai.getID();
		requestDetail(store_id);

	}

	private ResponseShopDetail detail;
	private void requestDetail(String id){
		if (!CommonUtils.isNetworkConnected(getApplicationContext())) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.M000));
			return;
		}
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.GET, API.shopDetail+"?id="+id, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				
				try {
					if (responseInfo.result != null) {
						Gson gson = new Gson();
						detail = gson.fromJson(responseInfo.result, ResponseShopDetail.class);
						waimai_order_shopNameTextView.setText(detail.NAME);
						waimai_order_shopAddrTextView.setText(detail.ADDRESS);
						waimai_order_advancePriceTextView.setText(detail.PRICE+"元起");
						ImageLoader.getInstance().displayImage(detail.PIC_URL, waimai_order_infoImageView , getDisplayOption());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				System.out.println(msg);
			}
		});
	}

	private void init() {
		Injector.getInstance().inject(this);
	//	setView();
		waimai_order_titlebar_left_btn.setOnClickListener(this);
		waimai_order_commitOrderTextView.setOnClickListener(this);
		waimai_order_time_Layout.setOnClickListener(this);


		waimai_order_info_Layout.getBackground().setAlpha(229);//90%透明度
		waimai_order_time_Layout.getBackground().setAlpha(204);//80%透明度
		waimai_order_name_Layout.getBackground().setAlpha(153);//60%透明度
		waimai_order_phone_Layout.getBackground().setAlpha(127);//50%透明度
		waimai_canting_address_Layout.getBackground().setAlpha(127);//50%透明度
		waimai_order_address_Layout.getBackground().setAlpha(127);//50%透明度
		waimai_order_remarks_Layout.getBackground().setAlpha(102);//40%透明度
		waimai_order_commitOrderTextView.getBackground().setAlpha(153);//60%透明度
		waimai_order_infoTexts_Layout.getBackground().setAlpha(235);//10%透明度

	}

	public void onClick(View v){
		switch (v.getId()) {
		//退出
		case R.id.waimai_order_titlebar_left_btn:
			back();
			break;
			//选择时间
		case R.id.waimai_order_time_Layout:

			if (processFlag == true) {
				dateTimePickerDialog();  
				processFlag = false;
				new TimeThread().start(); 
			}

			break;
			//点击提交
		case R.id.waimai_order_commitOrderTextView:
			waimaitime = waimai_order_timeTextView.getText().toString().trim();
			name = waimai_order_nameEditText.getText().toString().trim();
			phone = waimai_order_phoneEditText.getText().toString().trim();
			canting_address = waimai_canting_addressEditText.getText().toString().trim();
			address = waimai_order_addressEditText.getText().toString().trim();
			remark = waimai_order_remarksEditText.getText().toString().trim();
			
			if (CommonUtils.isNull_nie(waimaitime)||
					waimaitime.equals(getResources().getString(R.string.laoren_arrive_time))) {
				NewToast.makeText(getApplicationContext(),"送餐时间不能为空");
				return;
			}
			if ("".equals(CommonUtils.isnull(name))) {
				NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_name));
				return;
			}
			if (!CommonUtils.isMobile(phone)) {
				NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_phone));
				return;
			}
			if ("".equals(CommonUtils.isnull(canting_address))) {
				NewToast.makeText(getApplicationContext(), "餐厅地址不能为空");
				return;
			}
			if ("".equals(CommonUtils.isnull(address))) {
				NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_address));
				return;
			}
			showConfirmDialog();
			break;


		default:
			break;
		}

	}

	private void showConfirmDialog() {

		dlg = new LaoBanDialog(WaiMaiOrderActivity.this);
		if (dlg.isShowing()) {
			dlg.dismiss();
			return;
		}
		dlg.onCreate();
		dlg.setOnClickOK(new OnClickOKListener() {

			@Override
			public void setOnClickOK() {
//				Intent in = new Intent(WaiMaiOrderActivity.this,ConfirmOrderActivity.class);
//				in.putExtra("name", name);
//				in.putExtra("phone", phone);
//				in.putExtra("address", address);
//				in.putExtra("waimaitime", waimaitime);
//				in.putExtra("remark", remark);
//				in.putExtra("store_id", store_id);
//				in.putExtra("price", waimai_order_advancePriceTextView.getText().toString().trim());
//				startActivity(in, true);
//				dlg.dismiss();
//				finish();
				dlg.dismiss();
				RequestOrder requestOrder = new RequestOrder();
				requestOrder.ORDER_TYPE = "WMDD";
				requestOrder.contactor_address = address;
				requestOrder.contactor_person = name;
				requestOrder.contactor_phone = phone;
				requestOrder.remark = remark;
				requestOrder.reservation_time = waimaitime;
				requestOrder.store_id = store_id;
				requestOrder.STORE_ADDRESS = canting_address;
				requestOrder.SEND_ADDRESS = address;
				//requestOrder.price = waimai_order_advancePriceTextView.getText().toString().trim();
				API.submitOrder(requestOrder, WaiMaiOrderActivity.this);
			}
		});

	}
	private void dismisMyDialog() {
		if(  dlg != null && dlg.isShowing() ){
			dlg.dismiss();
		}
		dlg = null;

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		dismisMyDialog();
	}


	//选择时间
	public AlertDialog dateTimePickerDialog(){  
		LinearLayout dateTimeLayout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.date_time_picker, null);  
		datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.DatePicker);  
		timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.TimePicker);  
		timePicker.setIs24HourView(true);  

		OnTimeChangedListener timeListener= new OnTimeChangedListener() {  
			@Override  
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {  
				time.set(Calendar.HOUR_OF_DAY, hourOfDay);  
				time.set(Calendar.MINUTE, minute);  

			}  
		};  

		timePicker.setOnTimeChangedListener(timeListener);  
		OnDateChangedListener dateListener = new OnDateChangedListener() {  

			@Override  
			public void onDateChanged(DatePicker view, int year, int monthOfYear,  
					int dayOfMonth) {  
				time.set(Calendar.YEAR, year);  
				time.set(Calendar.MONTH, monthOfYear);  
				time.set(Calendar.DAY_OF_MONTH, dayOfMonth);  
			}  
		};  

		datePicker.init(time.get(Calendar.YEAR), time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH), dateListener);  
		timePicker.setCurrentHour(time.get(Calendar.HOUR_OF_DAY));  
		timePicker.setCurrentMinute(time.get(Calendar.MINUTE));  

		dialog = new AlertDialog.Builder(this).setTitle("设置日期时间").setView(dateTimeLayout)  
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
					@Override  
					public void onClick(DialogInterface dialog, int which) {  

						time.set(Calendar.YEAR, datePicker.getYear());  
						time.set(Calendar.MONTH, datePicker.getMonth());  
						time.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());  
						time.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());  
						time.set(Calendar.MINUTE, timePicker.getCurrentMinute());  
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
						waimai_order_timeTextView.setText(format.format(time.getTime()));
					}  
				}).setNegativeButton("取消", new DialogInterface.OnClickListener() {  
					@Override  
					public void onClick(DialogInterface dialog, int which) {  

					}  
				}).show();  
		return dialog;  
	} 

	private class TimeThread extends Thread {    
		public void run() {  
			try {  
				Thread.sleep(2000);  
				processFlag = true;  
			} catch (Exception e) {  
				e.printStackTrace();  
			}  
		}  
	} 





}
